/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package cn.ac.iie.di.ban.tools;

import java.util.*;
import java.util.HashMap;
import java.util.Map;
import org.apache.log4j.Logger;
import org.influxdb.InfluxDB;
import org.influxdb.InfluxDBFactory;
import org.influxdb.dto.BatchPoints;
import org.influxdb.dto.Point;

/**
 *
 * @author xiaoliang
 */
public class InfluxDBHelper {

    private static Logger logger = Logger.getLogger(InfluxDBHelper.class.getName());
    private String database;
    private String table;
    private InfluxDB influxDB;
    private String ip;
    private String username;
    private String password;

    public static synchronized InfluxDBHelper getInfluxDBHelper(String database, String table, String ip) {
        InfluxDBHelper influxDBHelper = new InfluxDBHelper(database, table, ip);
        influxDBHelper.connect();
        return influxDBHelper;
    }
    private InfluxDBHelper(String database, String table, String ip) {
        this.database = database;
        this.table = table;
        this.ip = ip;
    }

    public void connect() {
        try {
            if (influxDB == null) {
                influxDB = InfluxDBFactory.connect(ip);
                influxDB.createRetentionPolicy(table, database, "30d", "30m", 2, true);
            }
        } catch (Exception e) {
        }
    }

    public void batchInsert(List<Map<String, String>> msgs, String flag) {
        try {
            BatchPoints batchPoints = BatchPoints
                    .database(database)
                    .consistency(InfluxDB.ConsistencyLevel.ALL)
                    .build();
            for (Map<String, String> tag : msgs) {
                Long num = Long.parseLong(tag.remove(flag));
                Point point = Point.measurement(table).tag(tag).addField("value", num).build();
                batchPoints.point(point);
            }
            influxDB.write(batchPoints);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            influxDB = null;
        }
    }
    
    public static void main(String[] args) {
        String databases = "sxldb";
        String table = "data_size";
        String ip = "http://192.168.11.94:8086";

//        InfluxDBHelper influxDBHelper = new InfluxDBHelper(databases, table, ip);

//        influxDBHelper.connect();
        InfluxDBHelper influxDBHelper = InfluxDBHelper.getInfluxDBHelper(databases, table, ip);
        List<Map<String, String>> list = new ArrayList<>();
        List<Long> listnum = new ArrayList<>();
        for (int i = 0; i < 10; i++) {
            Map<String, String> msg = new HashMap<>();
            msg.put("opinion", "舆论场1");
            msg.put("scope", "4"+i);
            msg.put("source_tunnel", "fds");
            msg.put("source_type", "fds");
            msg.put("task_id", "fdasf");
            msg.put("uid", "hdf");
            Long num = System.nanoTime() % 10000;
//            msg.put("num", num + "");
            list.add(msg);
            listnum.add(1L);
        }
//        influxDBHelper.insert(list, listnum,"num");
//        for (Map<String, String> msgMap : list) {
//            System.out.println(msgMap.toString());
//        }
//        influxDBHelper.batchInsert(list, listnum, "num");

    }

}
